package com.mentaleye.controller;

import com.alibaba.fastjson.JSONObject;
import com.mentaleye.bean.CurriculumBean;
import com.mentaleye.biz.CurriculumBiz;
import com.mentaleye.biz.DictionaryBiz;
import com.mentaleye.enums.DictionaryEnums;
import com.mentaleye.util.CommonPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/curriculum")
public class CurriculumController {

    @Autowired
    private CurriculumBiz curriculumBiz;
    @Autowired
    private DictionaryBiz dictionaryBiz;

    @RequestMapping(value = "/curriculumList")
    public ModelAndView list() {
        ModelAndView view = new ModelAndView("tc/curriculum/curriculumList");

        Map<Integer,String>   classTypeMap=dictionaryBiz.queryDictionaryByParent(DictionaryEnums.CURRICULUM_TYPE.getCode());
        Map<Integer,String>   classStep=dictionaryBiz.queryDictionaryByParent(DictionaryEnums.CURRICULUM_STEP.getCode());
        view.addObject("classTypeMap", classTypeMap);
        view.addObject("classStepMap", classStep);
        view.addObject("classType",  JSONObject.toJSONString(classTypeMap));
        view.addObject("classStep",  JSONObject.toJSONString(classStep));
        return view;
    }

    @RequestMapping(value = "/curriculumEdit")
    public ModelAndView curriculumEdit(@RequestParam(value = "curriculumNo",required = false) Integer curriculumNo) {
        ModelAndView view = new ModelAndView("tc/curriculum/curriculumEdit");
        view.addObject("classTypeMap", dictionaryBiz.queryDictionaryByParent(DictionaryEnums.CURRICULUM_TYPE.getCode()));
        view.addObject("classStepMap", dictionaryBiz.queryDictionaryByParent(DictionaryEnums.CURRICULUM_STEP.getCode()));
        if(curriculumNo!=null){
            view.addObject("curriculum", curriculumBiz.getCurriculumByNo(curriculumNo));
        }
        return view;
    }

    @RequestMapping("/curriculumListData")
    @ResponseBody
    public String curriculumListData( CommonPage commonPage) {
        curriculumBiz.queryCurriculumByPage(commonPage);
        return commonPage.toSHowPage();
    }

    @RequestMapping(value = "/save")
    public @ResponseBody String save( CurriculumBean curriculumBeans) {
        return JSONObject.toJSONString(curriculumBiz.editCurriculum(curriculumBeans));
    }

    @RequestMapping(value = "/delete")
    public @ResponseBody String delete(@RequestBody List<Integer> ids) {
        return JSONObject.toJSONString(curriculumBiz.deleteCurriculum(ids));
    }

}